package com.gdth.log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class LogUtil {
	public static final String logServiceLogName = "Log.log";// 本服务输出的日志文件名称
	
	public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");// 日志名称格式
	
	/**
     * 去除文件的扩展类型（.log）
     * 
     * @param fileName
     * @return
     */
    public static String getFileNameWithoutExtension(String fileName) {
        return fileName.substring(0, fileName.indexOf("."));
    }
    
    /**
     * 根据ps命令得到的内容获取PID，User，name等信息
     * 
     * @param orgProcessList
     * @return
     */
    public static List<ProcessInfo> getProcessInfoList(List<String> orgProcessList) {
        List<ProcessInfo> procInfoList = new ArrayList<ProcessInfo>();
        for (int i = 1; i < orgProcessList.size(); i++) {
            String processInfo = orgProcessList.get(i);
            String[] proStr = processInfo.split(" ");
            // USER PID PPID VSIZE RSS WCHAN PC NAME
            // root 1 0 416 300 c00d4b28 0000cd5c S /init
            List<String> orgInfo = new ArrayList<String>();
            for (String str : proStr) {
                if (!"".equals(str)) {
                    orgInfo.add(str);
                }
            }
            if (orgInfo.size() == 9) {
                ProcessInfo pInfo = new ProcessInfo();
                pInfo.user = orgInfo.get(0);
                pInfo.pid = orgInfo.get(1);
                pInfo.ppid = orgInfo.get(2);
                pInfo.name = orgInfo.get(8);
                procInfoList.add(pInfo);
            }
        }
        return procInfoList;
    }
    
    /**
     * 获取本程序的用户名称
     * 
     * @param packName
     * @param allProcList
     * @return
     */
    public static String getAppUser(String packName, List<ProcessInfo> allProcList) {
        for (ProcessInfo processInfo : allProcList) {
            if (processInfo.name.equals(packName)) {
                return processInfo.user;
            }
        }
        return null;
    }
}
